module.exports = (dir, port) => {
  //导入 http 模块
  const http = require('http');
  const fs = require('fs');
  const {
    resolve
  } = require('path');
  const url = require('url');

  //创建服务对象  
  const server = http.createServer((request, response) => {
    //获取路径
    let {
      pathname
    } = url.parse(request.url, true);
    //解码 URL 路径  解决中文匹配不成功的问题
    pathname = decodeURI(pathname);
    //判断
    if (pathname === '/') {
      pathname = '/index.html';
    }
    //声明一个文件夹路径
    let root = dir; // 网站的根目录, 静态资源文件夹(目录)
    //拼接对应的文件路径  resolve('/abc', './css/app.js')   /abc/css/app.js
    //   resolve('/abc', 'D:/www','./css/app.css')  =>  D:/www/css/app.css
    let filePath = resolve(__dirname, root + pathname); // pathname    /index.html 
    console.log(filePath);
    //异步的方法读取
    fs.readFile(filePath, (err, data) => {
      //判断是否出错
      if (err) {
        response.end('<h1>404 Not Found</h1>');
        return;
      }
      //响应文件内容
      response.end(data);
    })
  });

  //监听端口, 启动服务   
  server.listen(port, '127.0.0.1', () => {
    console.log(`服务已经启动, ${port} 端口监听中.....`);
  });
}